package com.zhiwei.msf.gateway.service.impl;

import com.zhiwei.msf.gateway.dao.entity.AuthorityInfo;
import com.zhiwei.msf.gateway.dao.entity.RoleInfo;
import com.zhiwei.msf.gateway.dao.mapper.extmapper.ExtAuthorityInfoMapper;
import com.zhiwei.msf.gateway.service.AuthorityInfoService;
import com.zhiwei.msf.gateway.service.RoleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * function 授权服务
 * author zhiwei.yang
 * time 2020-2-27-10:58
 */
@Service
public class AuthorityInfoServiceImpl implements AuthorityInfoService {

    @Autowired
    private RoleInfoService roleInfoService;
    @Autowired
    private ExtAuthorityInfoMapper extAuthorityInfoMapper;

    @Override
    public List<AuthorityInfo> queryAuthorityByUserId(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return Collections.emptyList();
        }
        List<RoleInfo> roleInfoList = roleInfoService.queryRoleInfosByUserId(userId);
        if (CollectionUtils.isEmpty(roleInfoList)) {
            return Collections.emptyList();
        }
        List<String> roleIdList = roleInfoList.stream().map(RoleInfo::getUuid).collect(Collectors.toList());
        List<AuthorityInfo> authorityInfos = extAuthorityInfoMapper.queryAuthoritiesByRoleIds(roleIdList);
        return CollectionUtils.isEmpty(authorityInfos) ? Collections.emptyList() : authorityInfos;
    }
}